const initialState = {
    arr: [],
    shoparr: []
} // 快捷键rxx

let shopcarreducer = (state = initialState, { type, payload }) => {
    let newobj = JSON.parse(JSON.stringify(state))

    switch (type) {

        case "getlist":

            newobj.arr = payload
            return newobj
        case "addshop":
            // findIndex可以寻找数组中有没有符合条件的元素，如果有，返回该元素的下标，如果没有，返回 ——1
            let findeindex = newobj.shoparr.findIndex((item, index) => {
                return item.id === payload.id
            })
            if (findeindex !== -1) {
                alert("您已经添加过了")
            } else {
                newobj.shoparr.push(payload)
            }
            return newobj
        case "addFn" :
            newobj.shoparr.forEach((item)=>{
                if (item.id === payload) {
                    item.num ++
                }
            })
            return newobj
        default:
            return state
    }
}
export default shopcarreducer
